package bravo.xc.com.xc_bravo_android.operation.bankcard.addbank;

import android.util.Log;

import bravo.xc.com.xc_bravo_android.beans.AddBankResultBeans;
import bravo.xc.com.xc_bravo_android.beans.AddBeans;
import bravo.xc.com.xc_bravo_android.beans.AddResultBeans;
import bravo.xc.com.xc_bravo_android.http.HttpSingleton;
import bravo.xc.com.xc_bravo_android.http.UserOperationService;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * CREATED BY YANK
 * DATE : 2017/11/7
 * MAIL : YANK.TENYOND@GMAIL.COM
 * FUNCTION :
 */

public class AddBankcardPresenter extends AddBankcardContract.AddBankPresenter {

    private UserOperationService userOperationService = HttpSingleton.getInstance().service;


    @Override
    public void getSupportBank() {

        compositeSubscription.add(userOperationService.getSupportBank()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<AddBankResultBeans>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.e("@@onError",String.valueOf(e));
                        view.showToast("网络错误，请重试");
                    }

                    @Override
                    public void onNext(AddBankResultBeans addBankResultBeans) {
                        view.setAdapter(addBankResultBeans.getData().get(0).getBankNames());
                    }
                })
        );
    }



    @Override
    public void addBank( AddBeans addBeans) {
        compositeSubscription.add(userOperationService.addBank(addBeans)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<AddResultBeans>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.e("@@onError",String.valueOf(e));
                        view.showToast("网络错误，请重试");

                    }

                    @Override
                    public void onNext(AddResultBeans addResultBeans) {
                        if (addResultBeans.getCode() == 1){
                            view.showToast("添加成功");
                            view.setNull();
                        }else {
                            view.showToast(addResultBeans.getMsg());
                        }
                    }
                })
        );
    }


}
